page.tsx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { notFound } from "next/navigation";
  2. import { ReaderView } from "@/components/reader/reader-view";
  3. import { getBookById, getBookEntries } from "@/lib/book-catalog";
  4. type ReaderPageProps = {
  5. params: Promise<{
  6. bookId: string;
  7. }>;
  8. searchParams?: Promise<{
  9. chapter?: string;
  10. entry?: string;
  11. width?: string;
  12. theme?: string;
  13. font?: string;
  14. }>;
  15. };
  16. export default async function ReaderPage({ params, searchParams }: ReaderPageProps) {
  17. const { bookId } = await params;
  18. const resolvedSearchParams = searchParams ? await searchParams : undefined;
  19. const book = await getBookById(bookId);
  20. if (!book) {
  21. notFound();
  22. }
  23. const entries = getBookEntries(book);
  24. const rawEntryIndex = Number(resolvedSearchParams?.entry ?? resolvedSearchParams?.chapter ?? "0");
  25. const chapterIndex =
  26. Number.isFinite(rawEntryIndex) && rawEntryIndex >= 0 && rawEntryIndex < entries.length
  27. ? rawEntryIndex
  28. : 0;
  29. return (
  30. <ReaderView
  31. book={book}
  32. chapterIndex={chapterIndex}
  33. initialWidthKey={resolvedSearchParams?.width}
  34. initialThemeKey={resolvedSearchParams?.theme}
  35. initialFontKey={resolvedSearchParams?.font}
  36. />
  37. );
  38. }